﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QuanLyThuPhiTaiLieu.Biz;
using QuanLyThuPhiTaiLieu.Entity;
using Telerik.WinControls.UI;

namespace QuanLyThuPhiTaiLieu.Forms
{
    public partial class SearchResult : Form
    {
        public KhaiThacVaSuDungTaiLieu KhaiThacItem { get; set; }
        public ThamKhaoTaiLieu ThamKhaoItem { get; set; }
        public int SelectType { get; set; }

        public SearchResult(string name)
        {
            InitializeComponent();

            grdKhaiThac.AutoGenerateColumns = false;
            grdThamKhao.AutoGenerateColumns = false;

            BuildData(name);
        }

        public SearchResult(string name, int type)
        {
            InitializeComponent();

            grdKhaiThac.AutoGenerateColumns = false;
            grdThamKhao.AutoGenerateColumns = false;

            groupThamKhaoTaiLieu.Visible = false;
            this.Size = new System.Drawing.Size(this.Size.Width, this.Size.Height - 200);

            BuildDataKhaiThac(name);
        }

        public SearchResult()
        {
            InitializeComponent();
        }

        private void BuildData(string name)
        {
            var thamKhaoBiz = new ThamKhaoTaiLieuBiz();
            var data1 = thamKhaoBiz.SearchByName(name);

            var khaiThacBiz = new KhaiThacVaSuDungTaiLieuBiz();
            var data2 = khaiThacBiz.SearchByName(name);

            if (data2 != null && data2.Count > 0)
            {
                grdKhaiThac.DataSource = data2;

                if (data1 != null && data1.Count > 0)
                {
                    grdThamKhao.DataSource = data1;
                }
                else
                {
                    groupThamKhaoTaiLieu.Visible = false;
                    this.Size = new System.Drawing.Size(this.Size.Width, this.Size.Height - 200);
                }
            }
            else
            {
                groupKhaiThac.Visible = false;

                if (data1 != null && data1.Count > 0)
                {
                    groupThamKhaoTaiLieu.Location = new Point(groupKhaiThac.Location.X, groupKhaiThac.Location.Y);
                    grdThamKhao.DataSource = data1;
                }
                else
                {
                    groupThamKhaoTaiLieu.Visible = false;
                    this.Size = new System.Drawing.Size(this.Size.Width, this.Size.Height - 200);
                }
            }
        }

        private void BuildDataKhaiThac(string name)
        {
            var khaiThacBiz = new KhaiThacVaSuDungTaiLieuBiz();
            var data = khaiThacBiz.SearchByName(name);

            if (data != null && data.Count > 0)
            {
                grdKhaiThac.DataSource = data;                
            }            
        }

        private void SelectKhaiThac(object sender, EventArgs e)
        {            
            var item = grdKhaiThac.CurrentRow.DataBoundItem as KhaiThacVaSuDungTaiLieu;
            if (item != null)
            {
                KhaiThacItem = item;
                SelectType = 1;
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
        }

        private void SelectThamKhao(object sender, EventArgs e)
        {
            var item = grdThamKhao.CurrentRow.DataBoundItem as ThamKhaoTaiLieu;
            if (item != null)
            {
                ThamKhaoItem = item;
                SelectType = 2;
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
        }
    }
}
